package config;

import java.util.HashMap;
import java.util.Map;

public class ArgParser {
    private String[] args;

    public ArgParser(String[] args) {
        this.args = args;
    }

    public void parseArgs() {
        Map<String, String> argMap = new HashMap<>();

        try {
            for (int i = 0; i < args.length; i++) {
                switch (args[i]) {
                    case "-o":
                        if (i + 1 >= args.length) {
                            throw new IllegalArgumentException("-o 后必须提供输出文件路径");
                        }
                        argMap.put("output", args[++i]);
                        break;
                    case "-O1":
                        Config.is_O1 = true;
                        break;
                    case "-S":
                        break;
                    default:
                        if (argMap.containsKey("input")) {
                            throw new IllegalArgumentException("只能指定一个输入文件");
                        }
                        argMap.put("input", args[i]);
                        break;
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException("参数格式不正确");
        }

        // 验证必要参数
        if (!argMap.containsKey("input")) {
            throw new IllegalArgumentException("缺少输入文件路径");
        }

        Config.inputFile = argMap.get("input");
        Config.outputFile = argMap.get("output");
        String baseName = Config.outputFile.substring(0, Config.outputFile.lastIndexOf('.'));
        Config.lloutputFile = baseName + ".ll";
    }

}
